import { NativeModules } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';

const { DouBaoTTSModule } = NativeModules;

class DouBaoTTSService {
  private isInitialized: boolean = false;

  /**
   * 初始化豆包TTS
   */
  async initialize(): Promise<boolean> {
    try {
      if (this.isInitialized) {
        return true;
      }

      const result = await DouBaoTTSModule.initializeTTS();
      this.isInitialized = true;
      console.log('豆包TTS初始化成功');
      return result;
    } catch (error) {
      console.error('豆包TTS初始化失败:', error);
      throw error;
    }
  }

  /**
   * 合成音频文件
   */
  async synthesizeAudio(text: string, voice: string, outputPath: string): Promise<string> {
    try {
      if (!this.isInitialized) {
        await this.initialize();
      }

      const result = await DouBaoTTSModule.synthesizeAudio(text, voice, outputPath);
      console.log('音频合成成功:', result);
      return result;
    } catch (error) {
      console.error('音频合成失败:', error);
      throw error;
    }
  }

  /**
   * 检查是否已初始化
   */
  async checkInitialized(): Promise<boolean> {
    try {
      const result = await DouBaoTTSModule.isInitialized();
      this.isInitialized = result;
      return result;
    } catch (error) {
      console.error('检查初始化状态失败:', error);
      return false;
    }
  }

  /**
   * 销毁TTS
   */
  async destroy(): Promise<boolean> {
    try {
      const result = await DouBaoTTSModule.destroyTTS();
      this.isInitialized = false;
      console.log('销毁豆包TTS');
      return result;
    } catch (error) {
      console.error('销毁豆包TTS失败:', error);
      throw error;
    }
  }

  /**
   * 获取初始化状态
   */
  getInitializedState(): boolean {
    return this.isInitialized;
  }

  /**
   * 设置TTS配置
   */
  async setTTSConfig(appId: string, token: string): Promise<boolean> {
    try {
      const result = await DouBaoTTSModule.setTTSConfig(appId, token);
      console.log('TTS配置设置成功');
      return result;
    } catch (error) {
      console.error('TTS配置设置失败:', error);
      throw error;
    }
  }

  /**
   * 获取TTS配置
   */
  async getTTSConfig(): Promise<{ appid: string; token: string }> {
    try {
      const result = await DouBaoTTSModule.getTTSConfig();
      const config = JSON.parse(result);
      console.log('TTS配置获取成功');
      return {
        appid: config.appid || '',
        token: config.token || '',
      };
    } catch (error) {
      console.error('TTS配置获取失败:', error);
      return {
        appid: '',
        token: '',
      };
    }
  }

  /**
   * 初始化TTS配置（从本地存储加载）
   */
  async initializeTTSConfig(): Promise<boolean> {
    try {
      const storedConfig = await AsyncStorage.getItem('tts_config');
      if (storedConfig) {
        const config = JSON.parse(storedConfig);
        if (config.appid && config.token) {
          await this.setTTSConfig(config.appid, config.token);
          console.log('TTS配置已从本地存储初始化');
          return true;
        }
      }
      console.log('本地存储中没有TTS配置');
      return false;
    } catch (error) {
      console.error('初始化TTS配置失败:', error);
      return false;
    }
  }
}

export default new DouBaoTTSService(); 